/**
 * Created by zhaojm on 15/4/23.
 */
game.Box2dBall = game.BaseRole.extend({
    _sprite : null,
    _layer : null,
    _world : null,
    _body : null,
    _bodyDef : null,
    _fixtureDef:null,
    _className : "Box2dBall",
    ctor:function(p){
        this._super();

        var sprite = new cc.Sprite(res.ball_png);
        sprite.x = p.x;
        sprite.y = p.y;
        this._sprite = sprite;

        // Define the dynamic body.
        //Set up a 1m squared box in the physics world
        var b2BodyDef = Box2D.Dynamics.b2BodyDef
            , b2Body = Box2D.Dynamics.b2Body
            , b2FixtureDef = Box2D.Dynamics.b2FixtureDef
            , b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
            , b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
            , b2Vec2 = Box2D.Common.Math.b2Vec2;

        var bodyDef = new b2BodyDef();
        bodyDef.type = b2Body.b2_dynamicBody;
        bodyDef.position.Set(p.x / PTM_RATIO, p.y / PTM_RATIO);
        bodyDef.userData = this;
        this._bodyDef = bodyDef;


        // Define another box shape for our dynamic body.
        //var dynamicBox = new b2PolygonShape();
        //dynamicBox.SetAsBox(0.5, 0.5);//These are mid points for our 1m box
        var circle = new b2CircleShape();
        circle.SetRadius(sprite.getContentSize().height / 2 / PTM_RATIO);   // 半径
        // Define the dynamic body fixture.
        var fixtureDef = new b2FixtureDef();
        fixtureDef.shape = circle;//dynamicBox;
        fixtureDef.density = 1.0;   // 密度
        fixtureDef.friction = 0.0;  // 摩擦系数
        fixtureDef.restitution = 0.0;   // 弹性
        this._fixtureDef = fixtureDef;

    },

    addToLayer : function(layer, world){
        this._layer = layer;
        layer.addChild(this._sprite);
        this._world = world;


        this._body = this._world.CreateBody(this._bodyDef);
        this._body.CreateFixture(this._fixtureDef);

    },

    removeFromLayer:function(){
        this._sprite.removeFromParent();
        this._world.DestroyBody(this._body);
    },

    bounce : function(){
        //cc.log('bounce..');
        var v = this._body.GetLinearVelocity();
        v.y = v.y < 0 ? v.y : -v.y;
        //v.y = 0;
        v.y += -400;
        this._body.SetLinearVelocity(v);

        // 拍皮球
        //var b2Vec2 = Box2D.Common.Math.b2Vec2;
        //this._body.ApplyImpulse(new b2Vec2(0, -100), this._bodyDef.position);
        //this._body.ApplyForce(new b2Vec2(0, -10), this._bodyDef.position);
    },

    jump:function(){
        var v = this._body.GetLinearVelocity();
        v.y = 30;
        this._body.SetLinearVelocity(v);
    },

    //startRun:function(){
    //    var b2Vec2 = Box2D.Common.Math.b2Vec2;
    //    //this._body.ApplyImpulse(new b2Vec2(20, 0), this._bodyDef.position);
    //},


});